﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using MensUnoShop.Areas.Admin.Models;
using MensUnoShop.Models;
using SenerPro;
using MvcPaging;
using MensUnoShop.Areas.Admin.Filters;


namespace MensUnoShop.Areas.Admin.Controllers
{
    [ManagerCheckFilter]
    public class MemberManagementController : Controller
    {
        //
        // GET: /Admin/MemberManagement/
        int defaultPageSize = 30;
        private Logger _log = new Logger(Config.logpath);

        public ActionResult Index()
        {
            return View();
        }

        [HttpGet]
        public ActionResult MemberList(int? page)
        {
            int currentPageIndex = page.HasValue ? page.Value - 1 : 0;
            List<Member> memberlist = new List<Member>();
            try
            {
                if (page != 0)
                {
                    if (TempData["111"] != null)
                    {
                        foreach (var tt in TempData)
                        {
                            ViewData[tt.Key] = tt.Value;
                        }
                        Session["data"] = ViewData;

                        memberlist = (List<Member>)ViewData["memberlist"];
                    }
                    else
                    {
                        if (Session["data"] != null)
                        {
                            ViewData = (ViewDataDictionary)Session["data"];
                            memberlist = (List<Member>)ViewData["memberlist"];
                        }
                    }
                }
                else
                    currentPageIndex = 0;
                return View(memberlist.ToPagedList(currentPageIndex, defaultPageSize));
            }
            catch (Exception ex)
            {
                _log.RecordExceptionFormat(_log.FlowLogPath, "MemberList", ex.ToString());
            }
            return View();
            
        }

        [HttpPost]
        public ActionResult MemberList(FormCollection frm, int? page)
        {
            try
            {
                TempData["querychoice"] = frm["querychoice"] == null ? "" : frm["querychoice"].Trim().ToString();
                TempData["querycontent"] = frm["querycontent"] == null ? "" : frm["querycontent"].Trim().ToString();

                List<Member> memberlist = new List<Member>();
                memberlist = MemberFactory.GetMemberList(TempData["querychoice"].ToString(), TempData["querycontent"].ToString());

                ViewData["memberlist"] = memberlist;
                Session["memberlist"] = memberlist;//匯出excel用
                TempData["memberlist"] = memberlist;
                TempData["111"] = "1";
            }
            catch (Exception ex)
            {
                _log.RecordExceptionFormat(_log.FlowLogPath, "MemberList", ex.ToString());
            }

            return Redirect("MemberList");
        }


        public ActionResult MemberDetail(string id)
        {
            Member m = new Member();
            try
            {
                m = MemberFactory.GetMember(new Guid(id));
            }
            catch (Exception ex)
            {
                _log.RecordExceptionFormat(_log.FlowLogPath, "MemberDetail", ex.ToString());
            }

            return View(m);
        }

        public ActionResult MemberDetailView(string id)
        {
            Member m = new Member();
            try
            {
                m = MemberFactory.GetMember(new Guid(id));
            }
            catch (Exception ex)
            {
                _log.RecordExceptionFormat(_log.FlowLogPath, "MemberDetail", ex.ToString());
            }

            return View(m);
        }

        public string Api_changestatus(Guid mguid, string status)
        {
            bool ms;
            if (bool.TryParse(status, out ms))
            {
                MensUnoShop.Models.DBMember dbm = new DBMember();
                if (ms)
                    dbm.SetMemberOn(mguid);
                else
                    dbm.SetMemberOff(mguid);
                return "帳號變更完成";
            }
            else
                return "帳號變更失敗";
            
        }

    }
}
